package com.heima.behavior.service.Imp;
import java.util.Date;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import com.heima.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApUnlikeBehaviorServiceImp implements ApUnlikeBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        ApBehaviorEntry behaviorEntry = creatBehaviorEntryMsg(dto);
        if (behaviorEntry.getType() != 1) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"未登录请先登录");
            log.info("未登录请先登录");
        }
        //查询行为实体数据
        Query query = Query.query(Criteria.where("entryId").is(behaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        //判断行为是否存在
        if (unlikesBehavior == null && dto.getType() == 0) {
            //需要添加不喜欢
            unlikesBehavior = new ApUnlikesBehavior();
            unlikesBehavior.setEntryId(behaviorEntry.getId());
            unlikesBehavior.setArticleId(dto.getArticleId());
            unlikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(unlikesBehavior);
            return ResponseResult.okResult();
        }else {
            //存在 对值进行修改
            Update update = new Update();
            update.set("type", dto.getType());
            mongoTemplate.updateFirst(query,update,ApUnlikesBehavior.class);
            return ResponseResult.okResult();
        }
    }

    /**
     * 获取行为创建
     * @param dto
     * @return
     */
    private ApBehaviorEntry creatBehaviorEntryMsg(UnLikesBehaviorDTO dto) {
        //获得用户token中的id检查是否处于登陆状态
        ApUser user = AppThreadLocalUtils.getUser();
        //对数据进行判空
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"用户未携带ID或未携带设备ID");
        }
        //调用
        // return  ApBehaviorEntryService(user.getId(),likesBehaviorDTO.getEquipmentId());
        return apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(),dto.getEquipmentId());
    }
}
